package com.example.wangzh.developkit.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import com.example.wangzh.developkit.R;
import com.example.wangzh.developkit.constant.MathConstant;

/**
 * Created by USER on 2017/1/17.
 */

public class MeshNumberSelector extends AmountSelector {
	int maxMesh = 500, minMesh = 100;

	public void setMaxMesh(int maxMesh) {
		this.maxMesh = maxMesh;
		refreshMesh( );
	}

	public void setMinMesh(int minMesh) {
		this.minMesh = minMesh;
		refreshMesh( );
	}


	public interface OnAmountChangeListener {
		void OnAmountChange(double amount);
	}

	public MeshNumberSelector(Context context) {
		super(context);
	}

	public MeshNumberSelector(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void initView(Context context) {
		super.initView(context);
		txt_title.setText("目数");
		refreshMesh( );
	}

	private void refreshMesh() {
		txt_stock_amount.setText("(" + minMesh + "~" + maxMesh + "目)");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId( )) {
			case R.id.img_minus:
				performMinus();
				break;
			case R.id.img_plus:
				performPlus();
				break;
			case R.id.img_clear:
				setRequireAmount(0);
				edt_require_amount.setText("");
				break;
			default:
				break;
		}
	}

	protected void performPlus() {
		if (Math.abs(requireAmount - maxMesh) < MathConstant.SMALL) {
			return;
		}
		requireAmount += 1;
		if (requireAmount > maxMesh) {
			requireAmount = maxMesh;
		} else if (requireAmount < minMesh) {
			requireAmount = minMesh;
		}
		setRequireAmount(requireAmount);
		if (onAmountChangeListener != null) {
			onAmountChangeListener.OnAmountChange(requireAmount);
		}
	}

	protected void performMinus() {
		if (Math.abs(requireAmount - minMesh) < MathConstant.SMALL) {
			return;
		}
		requireAmount -= 1;
		if (requireAmount < minMesh) {
			requireAmount = minMesh;
		} else if (requireAmount > maxMesh) {
			requireAmount = maxMesh;
		}
		setRequireAmount(requireAmount);
		if (onAmountChangeListener != null) {
			onAmountChangeListener.OnAmountChange(requireAmount);
		}
	}
}
